package us.icitap.cm.ui.policecase;

import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.PoliceCode;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.business.entities.SupplementReport;
import us.icitap.cm.listeners.ReportListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.OfficerField;
import us.icitap.cm.ui.shared.PoliceCodeField;
import us.icitap.cm.ui.shared.PoliceOperationField;
import us.icitap.cm.ui.shared.ReportLayout;
import us.icitap.cm.utils.HandleNotification;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.FormatingUtils;
import us.icitap.utils.Result;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class SupplementReportDataLayout extends ReportLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6985073780384754588L;

	SupplementReport sr;

	private CustComboBox investStartedMethodSelect = new CustComboBox(AppListTypeEnum.InvestigationStartedMethod.getCaption());
	private CustTextField investStartedDescTextField = new CustTextField(AppListTypeEnum.InvestigationStartedMethod.getCaption());
	
	private  CustComboBox investMethodSelect = new CustComboBox(AppListTypeEnum.InvestigationMethod.getCaption());
	private CustTextField investMethDescTextField = new CustTextField(AppListTypeEnum.InvestigationMethod.getCaption());
	
	
	private OfficerField officerField = new OfficerField("Raporti plotesohet nga");
	private PoliceCodeField policeCodeField; 
	private PoliceOperationField policeOperationField = new PoliceOperationField();
	
	private CustTextArea notesTextArea = new CustTextArea("Permbledhje e ngjarjes");
	
	
//	private ReportRestrictionField reportRestrictionField = new ReportRestrictionField();
	
	private final CustBeanFieldGroup<SupplementReport> binder = new CustBeanFieldGroup<SupplementReport>(SupplementReport.class);

	final CustomFormLayout cfl = new CustomFormLayout("650px", "Raport Suplementar", "Reset", "Ruaj");
	private ReportListener listener;
	boolean editableVisible = false;
	boolean readOnlyVisible = false;
	
	final TabSheet tabSheet = new TabSheet();
	
	
	private void _init(){
		this.notesTextArea.setWidth("53em");
		this.notesTextArea.setRows(30);
		
		
		this.investStartedMethodSelect.setWidth("450px");
		this.investStartedDescTextField.setWidth("450px");
		this.investMethodSelect.setWidth("450px");
		this.investMethDescTextField.setWidth("450px");
	}
	
	
	public SupplementReportDataLayout() {
		this.setMargin(true);	
		
		_init();
		
		Result<List<PoliceCode>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getRootPoliceCode();
		if(result.isSucceeded())
			policeCodeField = new PoliceCodeField(result.getObject());
		else
			policeCodeField = new PoliceCodeField();
		
		
		investStartedMethodSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.InvestigationStartedMethod, false)));
		
		investMethodSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.InvestigationMethod, false)));

		
		
		FormLayout fl = new FormLayout();
		fl.addComponent(this.policeCodeField);
		fl.addComponent(this.investStartedMethodSelect);
		fl.addComponent(this.investStartedDescTextField);
		fl.addComponent(this.investMethodSelect);
		fl.addComponent(this.investMethDescTextField);
		fl.addComponent(this.policeOperationField);
		
		fl.addComponent(this.officerField);
//		fl.addComponent(this.reportRestrictionField);
		tabSheet.addTab(fl, "Informacion mbi ceshtjen");
		
		
		VerticalLayout vl = new VerticalLayout();
		vl.addComponent(notesTextArea);
		tabSheet.addTab(vl, "Permbledhje");
		
		
		cfl.addComponent(tabSheet);
//		cfl.addComponent(this.reportRestrictionField);
		cfl.addFooter();
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
	
					binder.commit();
					
					if(sr.getReport().getPoliceOperation() != null)
						sr.getReport().getPoliceOperation().setReport(sr.getReport());
					
					if(policeCodeField.getValue() == null || policeCodeField.getValue().isEmpty()){
						HandleNotification.showWarningOnScreen("Duhet te zgjidhni te pakten nje element nga kodi policor");
						return;
					}
					else{
						sr.getReport().setPrimaryPoliceCode(sr.getReport().getPoliceCodeList().get(0));
					}
						
					if(SupplementReportDataLayout.this.listener != null)
						SupplementReportDataLayout.this.listener.storeInMemory(sr.getReport());
					
					cfl.setRightButtonOkIcon();
				} catch (CommitException e) {

					e.printStackTrace();
				}
				
			}
		});
		
		binder.bind(investStartedMethodSelect, "report.investStartedMethod");
		binder.bind(investStartedDescTextField, "report.investStartedDescription");
		binder.bind(investMethodSelect, "report.investigationMethod");
		binder.bind(investMethDescTextField, "report.investMethodDescription");
		binder.bind(officerField, "report.reportOfficer");
		binder.bind(policeCodeField, "report.policeCodeList");
		binder.bind(policeOperationField, "report.policeOperation");
		binder.bind(notesTextArea, "report.notes");
//		binder.bind(reportRestrictionField, "report.reportRestrictionList");
	}
	
	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}
	
	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = new VerticalLayout(); //.showSR(sr);
		vl.setCaption("Raport Ngjarje");
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	
	
	@Override
	public void addReport(Report report) {
		sr= new SupplementReport();
		sr.setReport(report);
		sr.setSrUniqueNumber(report.getTempUniqueNumber());
		report.setSupplementReport(sr);
		
		binder.setItemDataSource(sr);
		officerField.setEnabled(false);
//		this.reportRestrictionField.setReport(report);
		this.showEditableForm();
	}

	@Override
	public void loadReport(Report report, boolean editable) {
		this.sr = report.getSupplementReport();
		binder.setItemDataSource(sr);
		if(editable) 
			this.showEditableForm();
		
		else
			this.showReadOnlyForm();
		
	}

	@Override
	public void addReportListener(ReportListener listener) {
		this.listener = listener;
		
	}



}
